//
//  main.cpp
//  Executor
//
//  Created by Chris Wong on 4/30/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "Executor.h"



#include<stdio.h>
#include<stdlib.h>
#include<syslog.h>
#include<unistd.h> 


//int main(void)
//{
//    pid_t pid;
//    int fd[2];
//    int wrfd[2];
//    pipe(fd);
//    pipe(wrfd);
//    
//    pid=fork();
//    if(pid == 0)     // child code
//    {
//        dup2(fd[1], STDOUT_FILENO); // Redirect stderr into writing end of pipe
//        char buf;
//        
//        if(dup2(wrfd[0], STDIN_FILENO) == -1)
//            perror("Failed to redirect stdin of writing");
//                // Redirect reading end of pipe into stdin
//        //while (read(wrfd[0], &buf, 1) > 0)
//            //write(STDIN_FILENO, &buf, 1);
//        
//        
//        // Now that we have copies where needed, we can close all the child's other references
//        // to the pipes.
//        close(fd[0]);
//        close(fd[1]);
//        close(wrfd[0]);
//        close(wrfd[1]);
//        
//        // First argument of command is nearly always the executable name
//        //execl("/usr/bin/grep","/usr/bin/grep","total", NULL);
//        execl("/bin/ls","/bin/ls","-l", NULL);
//
//        // Shouldn't reach here
//        exit(1);
//    }
//    else if(pid > 0)    // Parent code
//    {
//        int status;
//        close(fd[1]); // Don't need writing end of the stderr pipe in parent.
//        close(wrfd[0]); // Don't need the reading end of the stdin pipe in the parent
//        
//        // We can now read stderr from fd[0] and write to its stdin at wrfd[1].
//        // Beware that you can have a deadlock where the child's waiting on stdin while
//        // you're waiting for it to write to stderr!
//        std::string str = "/bin/";
//        char tmp;
//        int x = 0;
//        while((tmp=str[x])!= NULL){
//            write(wrfd[1], &tmp, 1);
//            x++;
//        }
//        std::cout << x << std::endl;
//        char buf;
//        wait(&status); // Wait for the child to quit so we don't leave a zombie
//        
//        std::cout << "enfd" <<std::endl;
//
//
//        while(read(fd[0], &buf, 1)>0)
//        {
//            std::cout << buf;
//        }
//        std::cout << "end" <<std::endl;
//        close(fd[0]); // Close the reading end of the stderr pipe
//        close(wrfd[1]); // Close the writing end of the stdout pipe
//        return(0);
//    }
//}
//
///*
int main (int argc, const char * argv[], char* envp[])
{
    char * argsd[] = {"-l" ,NULL};
    char * args[] = {"/bin/ls","-l" ,NULL};
    char * args2[] = {"/usr/bin/grep", "total",NULL};
    
    char ** commandList[] = {args, args2,NULL}; 
    
    
    Executor exe;
    threadInfo temp;
    
    exe.runThread(commandList, false,envp,temp);


    std::cout << "\n" <<std::endl;
    std::cout << "Processes[" << exe.psTrac.size() <<"]"<<std::endl;

    for(int x = 0; x < exe.psTrac.size(); x++){
        std::cout << exe.psTrac[x] <<std::endl;
    }
}

